package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userID = BaseContext.getCurrentId();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(userID);
        //13
        List<ShoppingCart> cart  = shoppingCartMapper.selete(shoppingCart);
        ShoppingCart result = new ShoppingCart();
        result.setUserId(userID);
        result.setCreateTime(LocalDateTime.now());
        if(cart != null && cart.size() != 0){
            BeanUtils.copyProperties(cart,result);
            result.setNumber(cart.get(0).getNumber()+1);
            shoppingCartMapper.update(result);
            return;
        }
        Long setmealId = shoppingCartDTO.getSetmealId();
        Long dishId = shoppingCartDTO.getDishId();
        result.setNumber(1);
        if(setmealId != null){
            SetmealVO setmealVO = setmealMapper.selectById(setmealId);
            result.setName(setmealVO.getName());
            result.setSetmealId(setmealVO.getId());
            result.setAmount(setmealVO.getPrice());
            result.setImage(setmealVO.getImage());
        }
        if(dishId != null){
            DishVO dishVO = dishMapper.fandById(dishId);
            result.setName(dishVO.getName());
            result.setDishId(dishVO.getId());
            result.setAmount(dishVO.getPrice());
            result.setImage(dishVO.getImage());
        }
        shoppingCartMapper.insert(result);
    }

    @Override
    public void clean() {
        Long userID = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userID);
    }

    @Override
    public List<ShoppingCart> list() {
        Long userID = BaseContext.getCurrentId();
        List<ShoppingCart> result  = shoppingCartMapper.selete(ShoppingCart.builder().userId(userID).build());
        return result;
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart result = shoppingCartMapper.selete(shoppingCart).get(0);
        Integer count = result.getNumber();
        if(--count > 0){
            result.setNumber(count);
            shoppingCartMapper.update(result);
        }else shoppingCartMapper.clean(result.getUserId());
    }
}
